home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / ALG5.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  142 lines

  1. /**************************************************************************
  2.  *
  3.  * alg5.cpp - Example programs for STL generic algorithms those producing
  4.  *    scalar values. Section 12.6
  5.  *
  6.  * $Id: alg5.cpp,v 1.3 1995/08/29 18:43:21 oberg Exp $
  7.  *
  8.  * $$RW_INSERT_HEADER "slyrs.cpp"
  9.  *
  10.  **************************************************************************/
  11.  
  12. # include <iostream.h>
  13.  
  14. # include <vector>
  15. # include <list>
  16. # include <algorithm>
  17. # include <numeric>
  18.  
  19. # include <string.h>
  20. # include <string>
  21. using namespace std;
  22.  
  23. // prototypes used to halt warnings
  24. bool isVowel (char);
  25. void count_example();
  26. void accumulate_example();
  27. template<class T>
  28. list<T> & listadd(list<T> & base, T & newValue);
  29. void inner_product_example();
  30. void equal_example();
  31.  
  32.  
  33. bool isVowel (char c)
  34. {
  35.     switch (c) {
  36.         case 'a': case 'A':
  37.         case 'e': case 'E':
  38.         case 'i': case 'I':
  39.         case 'o': case 'O':
  40.         case 'u': case 'U':
  41.             return true;
  42.         }
  43.     return false;
  44. }
  45.  
  46. void count_example()
  47.     // illustrate the use of the count function
  48. {
  49.     int ecount = 0;
  50.     int vowelCount = 0;
  51.     
  52.     char * text = "Now is the time to begin";
  53.     
  54.     count (text, text + strlen(text), 'e', ecount);
  55.     count_if (text, text + strlen(text), isVowel, vowelCount);
  56.     
  57.     cout << "There are " << ecount << " letter e's " << endl << "and "
  58.         << vowelCount << " vowels in the text:" << text << endl;
  59. }
  60.  
  61. list<int>& intReplicate (list<int>& nums, int n)
  62.     // add n to 1 to list
  63. {
  64.     while (n) nums.push_back(n--);
  65.     return nums;
  66. }
  67.  
  68. void accumulate_example()
  69.     // illustrate the use of the accumulate function
  70. {
  71.     int numbers[] = {1, 2, 3, 4, 5};
  72.     
  73.     int sum = accumulate(numbers, numbers+5, 0);
  74.     int product = accumulate(numbers, numbers+5, 1, times<int>());
  75.     
  76.     cout << "The sum of the first five numbers is " << sum << endl;
  77.     cout << "The product of the first five numbers is " << product << endl;
  78.     
  79.         // example with different types for init
  80.     list<int> nums;
  81.     nums = accumulate(numbers, numbers+5, nums, intReplicate);
  82.     copy (nums.begin(), nums.end(),
  83.         ostream_iterator<int>(cout, " ")), cout << endl;
  84. }
  85.  
  86. void inner_product_example()
  87.     // illustrate the use of the inner_product function
  88. {
  89.     int a[] = {4, 3, -2};
  90.     int b[] = {7, 3, 2};
  91.     
  92.         // example 1, simple inner product
  93.     int in1 = inner_product(a, a+3, b, 0);
  94.     cout << "Inner product is " << in1 << endl;
  95.     
  96.         // example 2, using different operations
  97.     bool anyequal = inner_product(a, a+3, b, true, logical_or<bool>(), equal_to<int>());
  98.     cout << "any equal? " << anyequal << endl;    
  99. }
  100.  
  101. void equal_example()
  102.     // illustrate the use of the equal function
  103. {
  104.     int a[] = {4, 5, 3};
  105.     int b[] = {4, 3, 3};
  106.     int c[] = {4, 5, 3};
  107.     
  108.     cout << "a = b is:" << equal(a, a+3, b) << endl;
  109.     cout << "a = c is:" << equal(a, a+3, c) << endl;
  110.     cout << "a pair-wise-greater_equal b is" << equal(a, a+3, b, greater_equal<int>()) << endl;
  111. }
  112.  
  113. void lexical_comparison_example ()
  114.     // illustrate the use of the lexical_comparison function
  115. {
  116.     char * wordOne = "everything";
  117.     char * wordTwo = "everybody";
  118.     
  119.     cout << "compare everybody to everything " <<
  120.         lexicographical_compare(wordTwo, wordTwo + strlen(wordTwo), wordOne, wordOne + strlen(wordOne))
  121.             << endl;
  122.             
  123.     int a[] = {3, 4, 5, 2};
  124.     int b[] = {3, 4, 5};
  125.     int c[] = {3, 5};
  126.     
  127.     cout << "compare a to b: " << lexicographical_compare(a, a+4, b, b+3) << endl;
  128.     cout << "compare a to c: " << lexicographical_compare(a, a+4, c, c+2) << endl;
  129. }
  130.  
  131. int main() {
  132.     cout << "STL generic algorithms -- algorithms that produce scalar results" << endl;
  133.     count_example();
  134.     accumulate_example();
  135.     inner_product_example();
  136.     equal_example();
  137.     lexical_comparison_example();
  138.     
  139.     cout << "End of scalar algorithms test"  << endl;
  140.     return 0;
  141. }
  142.